using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using StateMachine.GameStates;
using Rendering;

namespace StateMachine
{
    public class GameStateManager
    {
        private Stack<GameState> states;
        private Renderer renderer;
        private ContentManager content;
        private int screenWidth;
        private int screenHeight;
        private Game game;
        
        public Renderer Renderer
        {
            get { return renderer; }
            set { renderer = value; }
        }

        public ContentManager Content
        {
            get { return content; }
        }

        public GameStateManager(Renderer renderer, ContentManager content, 
                                int screenWidth, int screenHeight, Game game)
        {
            this.renderer = renderer;
            this.content = content;
            this.screenWidth = screenWidth;
            this.screenHeight = screenHeight;
            this.game = game;
            states = new Stack<GameState>();
        }

        public void ChangeState(GameState state)
        {
            states.Pop();
            PushState(state);
        }

        public void PushState(GameState state)
        {
            state.LoadContent(content);
            states.Push(state);
        }

        public void PopState()
        {
            states.Pop();
        }

        public void HandleInput(GameTime gameTime)
        {
            states.Peek().HandleInput(this,gameTime);
        }

        public void Update(GameTime gameTime)
        {
            if (states.Count > 0)
            {
                states.Peek().Update(this, gameTime);
            }
        }

        public void Draw(GameTime gameTime)
        {
            if (states.Count > 0)
              states.Peek().Draw(this, gameTime);
        }

        public void ExitGame()
        {
            game.Exit();
        }

    }
}
